package com.situ.gcbs.server.api;

import com.github.pagehelper.PageInfo;
import com.situ.gcbs.server.model.Member;
import com.situ.gcbs.server.model.searchbean.MemberSearchBean;
import com.situ.gcbs.server.service.MemberService;
import com.situ.gcbs.server.util.PaginateInfo;
import com.situ.gcbs.server.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 功能：
 *
 * @author 千堆雪
 * @version 1.0.0
 * @since 2024/5/16
 * <p>
 * created by 千堆雪 on 2024/5/16 10:19, last modified by 千堆雪 on 2024/5/16 10:19
 */
@Tag(name = "会员操作接口", description = "本类定义会员操作相关接口")
@RestController
@RequestMapping(value = "/api/v1/members", produces = "application/json")
public class MemberApi {
    private final Log LOGGER = LogFactory.getLog(MemberApi.class);//spring-jcl包使用的是org.apache.commons.logging包
    private MemberService memberService;

    @Autowired
    public void setMemberService(MemberService memberService) {
        this.memberService = memberService;
    }

    /**
     * 查询所有会员
     *
     * @return 所有会员实体
     */
    @Operation(method = "GET", summary = "会员查询接口", description = "本接口用于实现会员的查询",
            parameters = {
                    @Parameter(name = "page", description = "页码，整型，最小值为1", allowEmptyValue = true),
                    @Parameter(name = "limit", description = "页面大小，整型，最小值为1", allowEmptyValue = true)
            })
    @GetMapping({"/{a0}/{a1}", "/{a0}", "/", ""})
    public Result list(Integer page, Integer limit,
                       @PathVariable(required = false) Integer a0,
                       @PathVariable(required = false) Integer a1, MemberSearchBean msb) {
        LOGGER.info("接受请求，开始查询...");

        if (page == null) {
            page = a0 == null ? 1 : a0;
        }
        if (limit == null) {
            limit = a1 == null ? 20 : a1;
        }

        PaginateInfo pi = PaginateInfo.of(page, limit);

        List<Member> members = memberService.findAll(pi, msb);

        PageInfo<?> pif = (PageInfo) pi.getPayload();

        return Result.success(Map.of("members", members, "total", pif.getTotal(),
                "page", pi.getPageNo(), "limit", pi.getLimit()));
    }

    /**
     * 批量删除
     */
    @Operation(method = "DELETE", summary = "批量删除功能", description = "本接口用以实现会员的删除功能")
    @DeleteMapping
    public Result deleteByIds(@RequestBody Integer[] ids, Boolean logic, Boolean force) {
        Objects.requireNonNull(ids);

        //此处不演示逻辑删除
        int count = memberService.deleteByIds(ids, Boolean.TRUE.equals(force), Boolean.TRUE.equals(logic));

        //负数状态码表示业务错误。-300表示部分删除操作失败
        return Result.success(count > 0 ? 200 : -300, count > 0 ? "删除成功" : "删除失败", count);
    }

    /**
     * 保存会员实体
     *
     * @return 响应结果
     */
    @Operation(method = "POST", summary = "保存会员信息", description = "保存会员信息")
    @PostMapping
    public Result save(@RequestBody() Member member) {
        Objects.requireNonNull(member);

        boolean success = memberService.save(member);

        //负数状态码表示业务错误。-100表示新增操作失败
        return Result.success(success ? 200 : -100, success ? "操作成功" : "操作失败", member);
    }

    /**
     * 完整修改会员实体
     *
     * @param member 会员实体
     * @return 响应结果
     */
    @PutMapping
    public Result update(@RequestBody() Member member) {
        Objects.requireNonNull(member);
        boolean success = memberService.update(member);

        //负数状态码表示业务错误。-200表示完整修改操作失败
        return Result.success(success ? 200 : -200, success ? "操作成功" : "操作失败", member);
    }

    /**
     * 部分修改会员实体
     *
     * @param member 会员实体
     * @return 响应结果
     */
    @PatchMapping
    public Result patch(@RequestBody() Member member) {
        Objects.requireNonNull(member);

        boolean success = memberService.patch(member);

        //负数状态码表示业务错误。-201表示部分修改操作失败
        return Result.success(success ? 200 : -201, success ? "操作成功" : "操作失败", member);
    }
}
